package com.wn.service.impl;

import com.wn.bean.Owner;
import com.wn.bean.PageVo;
import com.wn.dao.OwnerDao;
import com.wn.dao.impl.OwnerDaoImp;
import com.wn.service.OwnerService;

import java.sql.SQLException;
import java.util.List;

/**
 * @author yellow Docter
 * @date 2024 -06-19
 * @desc
 */
public class OwnerServiceImpl implements OwnerService {

    private OwnerDao ownerDao = new OwnerDaoImp();

    @Override
    public boolean deleteOwnerByIds(String[] ids) {
        int row = 0;
        try {
            row = ownerDao.deleteOwnerByIds(ids);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return row > 0;
    }

    @Override
    public PageVo page(String currentPageStr, String pageSizeStr) {
        //01 拿到 分页的数据  currentPage，pageSize
        int currentPage = Integer.parseInt(currentPageStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        //02 查询总数
        int total = ownerDao.count();
        //03 分页查询
        int start = (currentPage - 1) * pageSize;
        List<Owner> list = ownerDao.page(start, pageSize);
        //04 组装前端的页面数据PageVo
        PageVo pageVo = new PageVo();
        pageVo.setData(list);
        pageVo.setTotal(total);
        return pageVo;
    }

    @Override
    public boolean saveOwner(Owner owner) {
        int row = 0;
        try {
            row = ownerDao.insertOwner(owner);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return row > 0;
    }

    @Override
    public List<Owner> listOwner() {
        return ownerDao.list();
    }

    @Override
    public boolean deleteOwnerById(int id) {
        int row = 0;
        try {
            row = ownerDao.deleteOwnerById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return row > 0;
    }

    @Override
    public boolean updateOwner(Owner owner) {
        int row = 0;
        try {
            row = ownerDao.updateOwner(owner);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return row > 0;
    }
}
